<?php
function wmlHeader($title) 
{
	header("Content-type: text/vnd.wap.wml; charset=utf-8");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n".
	"<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.3//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\">\n".
	"<wml>\n".
	"<head>\n".
	"<meta http-equiv=\"cache-control\" content=\"max-age=180,private\" />\n".
	"</head>\n".
	"<card id=\"dcWml\" title=\"$title\">\n".
	"<p>\n";
}

function menus() 
{
	global $lang, $cats;

	foreach($cats['info'] as $key=>$val) 
	{
		$html .= '<a href="index.php?action=list&amp;catcode='.$val['catcode'].'">'.$val['catname'].'</a> ';
	}
	$html .= "<br/><br/>";
	return $html;
}

function shopcats($catcode){
    
    $html .= "按分类查看<br/>";
    foreach($CAT AS $k=>$v){
    	if($v['module']=='shop' && strlen($k)==2) $html .= "<a href=\"index.php?action=shoplist&amp;catcode=$k\">$v[catname]</a> ";
    }
    $html .= "<br/><br/>";
}


function wmlFooter() 
{
	echo "<small>Powered by Legoule.Com</small>\n".
	"</p>\n".
	"</card>\n".
	"</wml>";
}

function submodelcats($modelid = 1)
{
	global $CAT;
	$subcat = array();
	foreach($CAT as $id=>$cat)
	{
		if($cat['modelid'] == $modelid) $subcat[$id] = $cat;
	}

	return $subcat;
}

function  wml_pages($num, $curr_page, $mpurl, $perpage = 20) 
{ 
	global $lang;
	if($num > $perpage) 
	{ 
		$page = 10; 
		$offset = 2; 
		$pages = ceil($num / $perpage); 
		$from = $curr_page - $offset; 
		$to = $curr_page + $page - $offset - 1; 
		if($page > $pages) 
		{ 
			$from = 1; 
			$to = $pages; 
		} 
		else 
		{ 
			if($from < 1) 
			{ 
				$to = $curr_page + 1 - $from; 
				$from = 1; 
				if(($to - $from) < $page && ($to - $from) < $pages) 
				{ 
					$to = $page; 
				} 
			} 
			elseif($to > $pages) 
			{ 
				$from = $curr_page - $pages + $to; 
				$to = $pages; 
				if(($to - $from) < $page && ($to - $from) < $pages) 
				{ 
					$from = $pages - $page + 1; 
				} 
			} 
		} 
		$multipage .= '<a href="'.$mpurl.'1">&lt;&lt;</a>  '; 
		for($i = $from; $i <= $to; $i++) 
		{ 
			if($i != $curr_page) 
			{ 
				$multipage .= '<a href="'.$mpurl.$i.'">['.$i.']</a> '; 
			} 
			else 
			{ 
				$multipage .= '<u><b>['.$i.']</b></u> '; 
			} 
		} 
		$multipage .= $pages > $page ? "...<a href=\"$mpurl$pages\"> [$pages] &gt;&gt;</a>" : "<a href=\"$mpurl$pages\">&gt;&gt;</a>"; 
	}
	return $multipage; 
}

function wml_strip($string)
{
	$string = str_replace(array('&nbsp;', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;', '&'), array(' ', '&', '"', "'", '', '', '', '{<}', '{>}', '', '', '&amp;'), strip_tags($string,'<img><br>'));
	return str_replace(array('{<}', '{>}'), array('&lt;', '&gt;'), $string);
}

function checksubarea($area){
    global $AREA;
    $arealen = strlen($area);
    foreach($AREA AS $k=>$v){
        if((strlen($k)==$arealen+4) && (substr($k,0,$arealen)==$area)) return TRUE;
    }
    return FALSE;
}



function check_wap()
{
    if (strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0)
    {
        // Check whether the browser/gateway says it accepts WML.
        $br = "WML";
    }else{
            $browser=substr(trim($_SERVER['HTTP_USER_AGENT']),0,4);
            if ($browser=="Noki" || // Nokia phones and emulators
                $browser=="Eric" || // Ericsson WAP phones and emulators
                $browser=="WapI" || // Ericsson WapIDE 2.0
                $browser=="MC21" || // Ericsson MC218
                $browser=="AUR"  || // Ericsson R320
                $browser=="R380" || // Ericsson R380
                $browser=="UP.B" || // UP.Browser
                $browser=="WinW" || // WinWAP browser
                $browser=="UPG1" || //UP.SDK 4.0
                $browser=="upsi" || // another kind of UP.Browser ??
                $browser=="QWAP" || // unknown QWAPPER browser
                $browser=="Jigs" || // unknown JigSaw browser
                $browser=="Java" || // unknown Java based browser
                $browser=="Alca" || // unknown Alcatel-BE3 browser (UP based?)
                $browser=="MITS" || // unknown Mitsubishi browser
                $browser=="MOT-" || // unknown browser (UP based?)
                $browser=="My S" ||//  unknown Ericsson devkit browser ?
                $browser=="WAPJ" || //  Virtual WAPJAG www.wapjag.de
                $browser=="fetc" || //  fetchpage.cgi Perl script from www.wapcab.de
                $browser=="ALAV" || //  yet another unknown UP based browser ?
                $browser=="Wapa" || //  another unknown browser (Web based "Wapalyzer"?)
                $browser=="UCWEB" || //  UCWEB
                $browser=="Symbian" || //  Symbian
                $browser=="Opera") // Opera
            {
                $br = "WML";
            }else{
                $br = "HTML";
            }
    }

    if($br =="WML"){
        return TRUE;
    }else{
        return FALSE;
    }
}

?>